﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using aerowan.common.Network;
using aerowan.server.Database;

namespace aerowan.server.Network.Packets
{
    public class CSPkt_Login : BaseRemotePacket
    {
        public int AccountID;
        public string Password;
        public string Callsign;

        public CSPkt_Login(byte[] data)
            : base(data)
        {

        }

        public override void ReadPacket(NetConnection con)
        {
            this.ReadByte();
            AccountID = this.ReadInt();
            this.ReadByte();
            Password = this.ReadString();
            this.ReadByte();
            Callsign = this.ReadString();
        }

        public override void RunPacket(NetConnection con)
        {
            Console.WriteLine("Processing Auth Request: " + AccountID + " / " + Password);
            bool authResult = AccountsDAO.Authenticate(AccountID, Password);
            if (!authResult)
            {
                con.CloseAtNextPacket();
                con.SendPacket(new SCPkt_Login() { Success = false });
                return;
            }
            // check callsign
            if (PilotLayer.Instance.CallsignExists(Callsign))
            {
                con.CloseAtNextPacket();
                con.SendPacket(new SCPkt_Login() { Success = null });
                return;
            }
            // all ok
            con.SendPacket(new SCPkt_Login() { Success = true });
        }
    }
}
